﻿using UnityEngine;
using System.Collections.Generic;
public class GameAssetManifest //: IAssetManifest
{
    static GameAssetManifest _instance;
    static public GameAssetManifest Instance
    {
        get {
            if (null == _instance)
                _instance = new GameAssetManifest();
            return _instance;
        }
    }
    AssetBundleManifest _abManifest;
    Dictionary<string, int> _beUsedCount = new Dictionary<string, int>();
    GameAssetManifest() { }
    public AssetBundleManifest assetBundleManifest
    {
        get { return _abManifest; }
        set
        {
            _abManifest = value;
            if (null == _abManifest)
                return;
            var allAbs = _abManifest.GetAllAssetBundles();
            int count;
            string bundleName;
            string depName;
            int loop;
            for (int i = 0; i < allAbs.Length; ++i)
            {
                bundleName = allAbs[i];
                var deps = _abManifest.GetDirectDependencies(bundleName);
                loop = null == deps ? 0 : deps.Length;
                for (int j = 0; j < loop; ++j)
                {
                    depName = deps[j];
                    _beUsedCount.TryGetValue(depName, out count);
                    _beUsedCount[depName] = count + 1;
                }

            }
        }
    }
    public bool enableAssetBundle { get { return false; } }
    public bool searchPersistPath { get { return false; } }
    public bool enableRemoteLoad { get { return false; } }
    public bool enalbePersistCache { get { return false; } }
    public int tryLoadCount { get { return 3; } }
    public string resourcePath { get { return string.Empty; } }//"Assets/Resources"
    string _steamPath;
    public string streamPath
    {
        get
        {
            if (!string.IsNullOrEmpty(_steamPath))
                return _steamPath;
            if (Application.isEditor)
            {
                _steamPath = "file:///" + Application.dataPath.Substring(0, Application.dataPath.Length - "Assets".Length) + "AssetBundles/android/";
                return _steamPath;
            }
            _steamPath = "file:///" + Application.streamingAssetsPath + "/android/";
            return _steamPath;
        }
    }
    public string persistPath { get { return Application.persistentDataPath; } }
    public string remotePath { get { return string.Empty; } }
    public bool existsInStream { get { return false; } }
    public bool existsInPersist { get { return false; } }
    public bool existsInNewest { get { return false; } }
    public int numLoadWorks { get { return 5; } }
    public int delayUnloadSeconds { get { return 5; } }
    public bool TryGetAssetBundleName(string assetPath, out string name)
    {
        int index = assetPath.LastIndexOf(".ab");

        name = assetPath;
        if (enableAssetBundle)
        {
            if (-1 == index)
                name = assetPath.ToLower() + ".ab";
            else
                name = assetPath.ToLower();

        }
        else
        {
            if (-1 == index)
                name = assetPath;
            else
                name = assetPath.Substring(0, index);
        }

        return false;
    }
    public string GetAssetBundleName(string assetPath)
    {
        string bundleName;
        bool hasSpecified = TryGetAssetBundleName(assetPath, out bundleName);
        return bundleName;
    }
    public string[] GetDirectDependencies(string assetBundleName)
    {
        if (null == assetBundleManifest)
            return new string[0];
        return assetBundleManifest.GetDirectDependencies(assetBundleName);
    }
    public string[] GetAllDependencies(string assetBundleName)
    {
        if (null == assetBundleManifest)
            return new string[0];
        return assetBundleManifest.GetAllDependencies(assetBundleName);
    }
    public int GetAssetBundleDirectUsageCount(string assetBundleName)
    {
        int count = 0;
        _beUsedCount.TryGetValue(assetBundleName, out count);
        return count;
    }
    public void OnCacheAsset(string path)
    {

    }
}